var app = getApp()
var utils = require('../../utils/utils.js')
Page({
  data: {
    showTipWords:false,
    cancelSearch: false,
    mobile: "",
    tipWords: '没有匹配的订单'
  },
  onLoad: function(options) {
    var that = this
    // 获取用户信息
    wx.login({
      success: function (res) {
        //调用后台接口
        var param ={"code":res.code,"appId":app.globalData.appId};
        wx.request({
          url: utils.sysConst.getDeliveryClerkInfoPath,
          method: 'POST',
          header: {
            "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
          },
          data: param,
          dataType: "json",
          success: function(res) {
            if (res.data.code == 1){
              //设置全局用户信息-openid
              app.globalData.openid = res.data.data.openId
              if(res.data.data.register && res.data.data.status == 1){
                that.setData({
                  cancelSearch: true,
                })
              }else{
                that.setData({
                  tipWords: "尚未注册配送员或账号不可用",
                  userOrder: null,
                  cancelSearch:false,
                  showTipWords:true,
                })
              }
            }else{
              wx.showToast({
                title:"获取用户信息失败："+res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
        // == getUserInfo end
      }
    });
    //wx.login === end ===
  },
  searchDeliveryClerkLog:function(event){
    if (!this.data.cancelSearch){
      wx.showToast({
        title:"尚未注册配送员或账号不可用",
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    var that = this
    var mobile = event.detail.value.mobile;
    if (mobile == "" || mobile == null){
      wx.showToast({
        title:"请输入订单手机号查询",
        icon: 'none',
        duration: 2000
      })
      that.setData({
        userOrder: null,
      })
      return false;
    }else{
      that.setData({
         mobile: mobile,
      })
      that.freshDeliveryClerkList();
    }
  },
  /**
   * 刷新当前页面数据
   * @param event
   */
  freshDeliveryClerkList:function(event){
    var that = this
    var param = {"openId":app.globalData.openid,"mobile":this.data.mobile}
    wx.request({
      url: utils.sysConst.getDeliveryLogPath,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",'appVersion':utils.appVersion,'signValue':utils.getSignValue(param)
      },
      data: param,
      dataType: "json",
      success: function(res) {
        if (res.data.code == 1 && res.data.data != null){
          that.setData({
            userOrder :res.data.data,
            showTipWords:false
          })
        }else{
          wx.showToast({
            title:"没有匹配的记录",
            icon: 'none',
            duration: 3000
          })

          that.setData({
            userOrder: null,
            showTipWords:true,
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


})

